/**
 * ExcelUtil.java
 * Description: Excel文件操作类
 * Created By Swiftly
 * Created On Dec 2, 2008
 * Version: 1.0
 *
 **/
package com.carsite.webapp.help;

import java.io.File;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.ListIterator;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExcelUtil {
	/**
	 * 根据list的内容来生成excel文件
	 * 
	 * @param fileName
	 *            excel文件名
	 * @param list
	 *            包含数据vo的list
	 * @param methods
	 *            vo的get方法
	 * @param cellTitle
	 *            列的描述
	 * @return
	 */
	public static boolean createExcelFromBeanList(String fileName, List list,
			String[] methods, String[] cellTitle) {
		return createExcelFromBeanList(fileName, "sheet0", list, methods,
				cellTitle);
	}
	public static boolean createExcelFromBeanList(String fileName, List list,
			int[] methods, String[] cellTitle) {
		return createExcelFromBeanList(fileName, "sheet0", list, methods,
				cellTitle);
	}
	public static boolean createExcelFromBeanList(String fileName, List list,
			int[] methods, String[] cellTitle,OutputStream os) {
		return createExcelFromBeanList(fileName, "sheet0", list, methods,
				cellTitle,os);
	}

	public static boolean createExcelFromBeanList(String fileName,
			String sheetName, List list, int[] methods, String[] cellTitle) {
		boolean ok = true;

		if ((null==fileName||"".equals(fileName) )|| list == null || methods == null
				|| cellTitle == null || methods.length != cellTitle.length)
			return false;

		WritableWorkbook wwb = null;
		try {
			File exlFile = new File(fileName);
			if (!exlFile.exists()) {
				exlFile.createNewFile();
			}
			
			
             // Workbook.createWorkbook(os);
			
			
			wwb = Workbook.createWorkbook(exlFile);
			
			///
			
			WritableSheet ws = wwb.createSheet(sheetName, 0);

			// 写表头描述
			int row = 0;
			for (int i = 0, len = cellTitle.length; i < len; i++) {
				Label label = new Label(i, row, cellTitle[i]);

				ws.addCell(label);
			}
			row += 1;

			// 写值
			ListIterator li = list.listIterator();
			while (li.hasNext()) {
				Object[] bean = (Object[])li.next();

				for (int i = 0; i < methods.length; i++) {
					 

					Object rst = bean[methods[i]] ;

					Label label = new Label(i, row, rst != null ? rst
							.toString() : "");

					ws.addCell(label);
				}

				row += 1;
			}
			
		/*	System.out.println("wwb.toString()");
			System.out.println(wwb.toString()); 
			System.out.println("wwb.toString()");
			File file=new File("");
			wwb.setOutputFile(file);
			file.
			 */
			
			
			wwb.write();
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		} finally {
			if (wwb != null) {
				try {
					wwb.close();
				} catch (Exception e) {

				}
			}
		}

		return ok;
	}
	
	public static boolean createExcelFromBeanList(String fileName,
			String sheetName, List list, int[] methods, String[] cellTitle,OutputStream os) {
		boolean ok = true;

		if ((null==fileName||"".equals(fileName) )|| list == null || methods == null
				|| cellTitle == null || methods.length != cellTitle.length)
			return false;

		WritableWorkbook wwb = null;
		try { 
			wwb =  Workbook.createWorkbook(os);
			
			
			//wwb = Workbook.createWorkbook(exlFile);
			
			///
			
			WritableSheet ws = wwb.createSheet(sheetName, 0);

			// 写表头描述
			int row = 0;
			for (int i = 0, len = cellTitle.length; i < len; i++) {
				Label label = new Label(i, row, cellTitle[i]);

				ws.addCell(label);
			}
			row += 1;

			// 写值
			ListIterator li = list.listIterator();
			while (li.hasNext()) {
				Object[] bean = (Object[])li.next();

				for (int i = 0; i < methods.length; i++) {
					 

					Object rst = bean[methods[i]] ;

					Label label = new Label(i, row, rst != null ? rst
							.toString() : "");

					ws.addCell(label);
				}

				row += 1;
			}
			
		/*	System.out.println("wwb.toString()");
			System.out.println(wwb.toString()); 
			System.out.println("wwb.toString()");
			File file=new File("");
			wwb.setOutputFile(file);
			file.
			 */
			
			
			wwb.write();
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		} finally {
			if (wwb != null) {
				try {
					wwb.close();
				} catch (Exception e) {

				}
			}
		}

		return ok;
	}
	/**
	 * 根据list的内容来生成excel文件
	 * 
	 * @param fileName
	 * @param sheetName
	 *            工作表名
	 * @param beanMap
	 * @param list
	 * @return
	 */
	public static boolean createExcelFromBeanList(String fileName,
			String sheetName, List list, String[] methods, String[] cellTitle) {
		boolean ok = true;

		if ((null==fileName||"".equals(fileName) )|| list == null || methods == null
				|| cellTitle == null || methods.length != cellTitle.length)
			return false;

		WritableWorkbook wwb = null;
		try {
			File exlFile = new File(fileName);
			if (!exlFile.exists()) {
				exlFile.createNewFile();
			}

			wwb = Workbook.createWorkbook(exlFile);
			WritableSheet ws = wwb.createSheet(sheetName, 0);

			// 写表头描述
			int row = 0;
			for (int i = 0, len = cellTitle.length; i < len; i++) {
				Label label = new Label(i, row, cellTitle[i]);

				ws.addCell(label);
			}
			row += 1;

			// 写值
			ListIterator li = list.listIterator();
			while (li.hasNext()) {
				Object bean = li.next();

				for (int i = 0, len = methods.length; i < len; i++) {
					String methodName = methods[i];
					Method method = bean.getClass().getMethod(methodName);

					Object rst = method.invoke(bean, null);

					Label label = new Label(i, row, rst != null ? rst
							.toString() : "");

					ws.addCell(label);
				}

				row += 1;
			}
			 
			wwb.write();
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		} finally {
			if (wwb != null) {
				try {
					wwb.close();
				} catch (Exception e) {

				}
			}
		}

		return ok;
	}
	
}
